import 'dart:async';

import 'package:flutter/material.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter_study/eventbus/bean.dart';
import 'package:flutter_study/eventbus/send_date.dart';
EventBus eventBus = new EventBus();
class EventBusPage extends StatefulWidget {
  final String title;

  EventBusPage({Key key, this.title}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return EventBusPageState();
  }
}

class EventBusPageState extends State<EventBusPage> {
  var _receiveDate = '';

  StreamSubscription loginSubscription;

  @override
  void initState() {

    loginSubscription = eventBus.on().listen((bean) {
      if (null != bean && bean.title == '收到数据') {
        setState(() {
          print("===>" + bean.title + bean.msg);
          _receiveDate = bean.msg;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text(this.widget.title),
      ),
      body: Column(
        children: <Widget>[
          RaisedButton(
            onPressed: () {
              Navigator.of(context)
                  .push(MaterialPageRoute(builder: (BuildContext context) {
                return SendData(
                  title: '发送数据界面',
                );
              }));
            },
            child: Text('跳转到发送界面'),
          ),
          Text('接收到的数据：'),
          Text(_receiveDate)
        ],
      ),
    );
  }

  @override
  void dispose() {
    if (null != eventBus) {
      loginSubscription.cancel();
      eventBus = null;
    }
  }
}
